package com.cskaoyan.market.admin.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cskaoyan.market.admin.service.AdminConfigService;
import com.cskaoyan.market.admin.service.MarketAdminConfigService;
import com.cskaoyan.market.admin.service.impl.AdminConfigServiceImpl;
import com.cskaoyan.market.admin.service.impl.MarketAdminConfigServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 配置模块控制层
 *
 * @author BlackFlame33
 * @date 2023/03/21
 **/
@WebServlet("/admin/config/*")
public class AdminConfigController extends HttpServlet {
    private MarketAdminConfigService configService = new MarketAdminConfigServiceImpl();

    private AdminConfigService adminConfigService = new AdminConfigServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if ("express".equals(op)) {
            getExpress(req, resp);
        }
        if ("order".equals(op)) {
            getOrder(req, resp);
        }
        if ("wx".equals(op)) {
            getWx(req, resp);
        }
        if ("mall".equals(op)) {
            getMallConfig(req, resp);
        }
    }

    /**
     * 这是一个wx的get方法分发
     *
     * @param req
     * @param resp
     */
    private void getWx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.getInitialWx();

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    /**
     * 这是一个order的get方法分发
     *
     * @param req
     * @param resp
     */
    private void getOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.getInitialOrder();

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    /**
     * 这是一个express的get方法
     *
     * @param req
     * @param resp
     */
    private void getExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Map<String, String> data = configService.getInitialExpress();

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    private void getMallConfig(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = adminConfigService.getMallConfig();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if ("express".equals(op)) {
            postExpress(req, resp);
        }
        if ("order".equals(op)) {
            postOrder(req, resp);
        }
        if ("wx".equals(op)) {
            postWx(req, resp);
        }
        if ("mall".equals(op)) {
            updateMallConfig(req, resp);
        }

    }

    /**
     * 这是一个Wx的配置修改方法
     *
     * @param req
     * @param resp
     */
    private void postWx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        Map<String, String> loadValue = new HashMap<>();
        String goods = JacksonUtil.parseString(requestBody, "market_wx_catlog_goods");
        String list = JacksonUtil.parseString(requestBody, "market_wx_catlog_list");
        String brand = JacksonUtil.parseString(requestBody, "market_wx_index_brand");
        String hot = JacksonUtil.parseString(requestBody, "market_wx_index_hot");
        String newGoods = JacksonUtil.parseString(requestBody, "market_wx_index_new");
        String topic = JacksonUtil.parseString(requestBody, "market_wx_index_topic");
        String share = JacksonUtil.parseString(requestBody, "market_wx_share");
        loadValue.put("market_wx_catlog_goods", goods);
        loadValue.put("market_wx_catlog_list", list);
        loadValue.put("market_wx_index_brand", brand);
        loadValue.put("market_wx_index_hot", hot);
        loadValue.put("market_wx_index_new", newGoods);
        loadValue.put("market_wx_index_topic", topic);
        loadValue.put("market_wx_share", share);

        configService.setWx(loadValue);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    /**
     * 这是一个order的配置修改方法
     *
     * @param req
     * @param resp
     */
    private void postOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String comment = JacksonUtil.parseString(requestBody, "market_order_comment");
        String unConfirm = JacksonUtil.parseString(requestBody, "market_order_unconfirm");
        String unpaid = JacksonUtil.parseString(requestBody, "market_order_unpaid");
        String keyNameComment = "market_order_comment";
        String keyNameUnConfirm = "market_order_unconfirm";
        String keyNameUnpaid = "market_order_unpaid";

        configService.setOrder(comment, unConfirm, unpaid, keyNameComment, keyNameUnConfirm, keyNameUnpaid);

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    /**
     * 这是一个express的配置修改方法
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void postExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String minMoney = JacksonUtil.parseString(requestBody, "market_express_freight_min");
        String value = JacksonUtil.parseString(requestBody, "market_express_freight_value");
        String keyName1 = "market_express_freight_min";
        String keyName2 = "market_express_freight_value";

        configService.setExpress(keyName1, keyName2, minMoney, value);

        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));

    }

    private void updateMallConfig(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        Map<String, String> map = objectMapper.readValue(requestBody, new TypeReference<Map<String, String>>() {});
        int code = adminConfigService.updateMallConfig(map);
        if (code != 0) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgumentValue()));
        } else {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
        }
    }
}